<template>
  <div>
    <el-form ref="loginform" class="center" :model="formData" :rules="rules">
      <el-form-item prop="mobile">
        <el-input v-model="formData.mobile" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="formData.password" />
      </el-form-item>
      <el-form-item prop="password2">
        <el-input v-model="formData.password2" />
      </el-form-item>
      <el-button @click="login">登录</el-button>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const validpass = (rule, value, callback) => {
      if (value !== this.formData.password) {
        callback(new Error('两次输入不一致'))
      } else {
        callback()
      }
    }
    return {

      formData: {
        mobile: '',
        password: '',
        password2: ''
      },
      rules: {
        mobile: [
          { required: true, message: '手机号必填', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码必填', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '再次输入密码', trigger: 'blur' },
          { validator: validpass, trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    async login() {
      // this.$refs.loginform.validate(boolean => {
      //   if (boolean) {
      //     console.log('登录成功')
      //   }
      // })
      // this.$refs.loginform.validate().then(() => {
      //   console.log('登录成功')
      // }).catch(
      //   () => { console.log('登录失败') }
      // )
      try {
        await this.$refs.loginform.validate()
        console.log('登录成功')
      } catch (error) {
        console.log('登录失败', error)
      }
    }
  }
}
</script>

<style>

.center{
  margin: 100px auto;
  width: 200px;
}
</style>
